<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="keyword" placeholder="关键词" />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList()"
        >查询</el-button
      >

      <el-button type="default" @click="resetData()">清空</el-button>
      <el-button
        type="success"
        @click="openAddForm"
        v-show="ROLE === 'admin' || ROLE === 'band'"
        >添加演唱会</el-button
      >
    </el-form>

    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="concerts"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row
    >
      <el-table-column prop="attended" label="报名" width="100" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            :disabled="scope.row.attended"
            @click="attendConcert(scope.row.id)"
          >
            {{ scope.row.attended ? "已报名" : "点击报名" }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="name" label="演唱会名称" width="160" />

      <el-table-column prop="place" label="举办地点" />
      <el-table-column prop="bandName" label="乐队" width="110" />

      <el-table-column
        prop="createTime"
        label="举办时间"
        type="datetime"
        width="160"
      >
      </el-table-column>

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="selectFans(scope.row.id)"
            >参加人员</el-button
          >

          <el-button
            type="text"
            @click="edit(scope.row.id)"
            v-show="ROLE === 'admin' || ROLE === 'band'"
            >编辑</el-button
          >

          <el-button
            type="text"
            @click="removeDataById(scope.row.id)"
            v-show="ROLE === 'admin' || ROLE === 'band'"
            >删除</el-button
          >
        </template></el-table-column
      ></el-table
    >

    <!--参加人员列表窗口 -->
    <el-dialog
      title="参加人员"
      :visible.sync="fansVisible"
      width="60%"
      height="80%"
    >
      <el-table :data="fans">
        <!-- 表格的其他表单项代码 -->
        <el-table-column prop="username" label="姓名" width="120" />
        <el-table-column prop="gender" label="性别" width="50">
          <template slot-scope="scope">
            <span v-if="scope.row.gender === 0">女</span>
            <span v-else>男</span>
          </template>
        </el-table-column>
        <el-table-column prop="age" label="年龄" width="50" />
        <el-table-column prop="email" label="邮箱" width="180" />
        <el-table-column prop="education" label="学历" width="80" />
        <el-table-column prop="profession" label="职业" width="80" />
      </el-table>
    </el-dialog>

    <!-- 编辑窗口 -->
    <el-dialog title="编辑演唱会" :visible.sync="editVisible">
      <el-form label-width="80px" :model="concert">
        <el-form-item label="名称">
          <el-input v-model="concert.name"></el-input>
        </el-form-item>
        <el-form-item label="举办地点">
          <el-input v-model="concert.place"></el-input>
        </el-form-item>
        <el-form-item label="举办时间">
          <el-date-picker
            v-model="concert.createTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="举办乐队">
          <el-select v-model="concert.bandId">
            <el-option
              v-for="item in bands"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateConcert">保 存</el-button>
      </div>
    </el-dialog>
    <!-- 添加窗口 -->
    <el-dialog title="添加演唱会" :visible.sync="dialogFormVisible">
      <el-form label-width="80px" :model="concert">
        <el-form-item label="名称">
          <el-input v-model="concert.name"></el-input>
        </el-form-item>
        <el-form-item label="举办地点">
          <el-input v-model="concert.place"></el-input>
        </el-form-item>
        <el-form-item label="举办时间">
          <el-date-picker
            v-model="concert.createTime"
            type="datetime"
            placeholder="选择日期时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="举办乐队">
          <el-select v-model="concert.bandId">
            <el-option
              v-for="item in bands"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addConcert">保 存</el-button>
      </div>
    </el-dialog>

    <!-- 分页 -->
    <el-pagination
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />
  </div>
</template>
    <script>
//引入js文件
import {
  getPageByCondition,
  add,
  deleteConcert,
  getFansByConcertId,
  update,
  get,
  updateAttendStatus,
} from "@/api/concert";
import { selectAll } from "@/api/band";
import { getToken } from "@/utils/auth";
import { getInfo } from "@/api/user";
export default {
  data() {
    return {
      ROLE: "",
      bands: [],
      editMemberVisible: false,
      fansVisible: false,
      editVisible: false,
      dialogFormVisible: false,
      fans: [], //参加人员
      concert: {
        name: "",
        place: "",
        bandId: "",
        createTime: "",
      },

      listLoading: true, //是否显示“数据加载中这几个字”

      concertId: 0,
      concerts: [], //封装返回的数据
      total: 0, //数据条数
      currentPage: 1, //当前页码
      pageSize: 5, //每页显示条数
      keyword: "",
    };
  },
  created() {
    this.setUserRole();
    this.getList();
    this.selectBands();
  },
  methods: {
    //设置用户角色
    setUserRole() {
      let userId = getToken();
      getInfo(userId).then((res) => {
        this.ROLE = res.data.roles[0];
      });
    },
    //报名演唱会
    attendConcert(id) {
      let userId = getToken();
      // console.log(id)
      this.$confirm("确定要参加此演唱会吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          updateAttendStatus(userId, id).then((res) => {
            this.$message.success("报名成功");
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消报名",
          });
        });
    },
    //查询有哪些乐队
    selectBands() {
      selectAll().then((res) => {
        this.bands = res.data.rows;
      });
    },

    //获取参加人员列表
    selectFans(id) {
      // console.log(id);
      //保存演唱会id
      this.concertId = id;
      //根据演唱会id查询参加者成员信息
      getFansByConcertId(id).then((res) => {
        this.fans = res.data.rows;
      });
      //打开对话框
      this.fansVisible = true;
    },
    //更新演唱会信息
    updateConcert() {
      update(this.concert)
        .then((res) => {
          this.$message.success("修改成功");
          this.editVisible = false;

          //刷新
          this.getList();
        })
        .catch((err) => {
          this.$message.error(err.data);
        });
    },
    //编辑
    edit(id) {
      //打开窗口
      this.editVisible = true;
      //查询演唱会信息
      get(id)
        .then((res) => {
          // console.log("回显成功");
          this.concert = res.data.row;
        })
        .catch((err) => {
          console.log("回显失败");
        });
    },
    //添加
    addConcert() {
      add(this.concert)
        .then((res) => {
          this.$message.success("新增演唱会成功");
          this.dialogFormVisible = false;
          this.concert = {}; //清空表格内容
          //刷新
          this.getList();
        })
        .catch((err) => {
          this.$message.error(err.data);
        });
    },
    //打开添加窗口
    openAddForm() {
      this.concert = {}; //清空表格内容
      this.dialogFormVisible = true;
    },
    //分页查询
    getList(currentPage = 1) {
      //默认第一页
      this.currentPage = currentPage;
      let userId = getToken();
      getPageByCondition(userId, this.currentPage, this.pageSize, this.keyword)
        .then((res) => {
          // console.log(res.data.rows);
          this.total = res.data.total;
          this.concerts = res.data.rows;
          // console.log(this.total)
          // console.log(this.list)
          this.listLoading = false; //将“数据加载中”这几个字关掉
        })
        .catch((error) => {
          console.log(error);
        });
    },
    handleChange(value) {
      // console.log(value);
    },
    resetData() {
      //清空查询条件
      this.keyword = "";
      this.getList();
    },
    removeDataById(id) {
      //删除方法
      //提示信息
      this.$confirm("此操作将永久删除该演唱会, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //调用删除方法
          deleteConcert(id).then((res) => {
            this.$message.success("删除成功");
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
  
  <style>
.form-container {
  display: flex;
  justify-content: flex-end;
}
</style>